package cn.itcast.crm.service;

import java.util.List;

import javax.ws.rs.Consumes;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.crm.dao.CustomerRepository;
import cn.itcast.crm.domain.Customer;

@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
	
	@Autowired
	private CustomerRepository customerRepository;

	@Override
	public List<Customer> findFiexedAreaCustomer(String fixedAreaId) {
		
		return customerRepository.findByFixedAreaId(fixedAreaId);
	}

	@Override
	public List<Customer> findNoCustomer() {
		
		return customerRepository.findByFixedAreaIdIsNull();
	}

	@Override
	public void CustomerToFixedArea(String customerIdStr, String fixedAreaId) {
		//先解除关联
		customerRepository.clearCustomerId(fixedAreaId);
		if(StringUtils.isBlank(customerIdStr) || "null".equals(customerIdStr)){
			return;
		}
		String[] customerId=customerIdStr.split(",");
		for(String ids:customerId){
			Integer id=Integer.parseInt(ids);
			customerRepository.updateFixedAreaId(fixedAreaId,id);
		}
	}

	@Override
	public void regist(Customer customer) {
		customerRepository.save(customer);
	}

	@Override
	public Customer findByTelephone(String telephone) {
		
		return customerRepository.findByTelephone(telephone);
	}

	@Override
	public void updataType(String telephone) {
		 customerRepository.updataType(telephone);
	}

	@Override
	public Customer login(String telephone, String password) {
		return customerRepository.findByTelephoneAndPassword(telephone,password);
	}

	@Override
	public String findFixedAreaByAddress(String address) {
		return customerRepository.findFixedAreaByAddress(address);
	}

}
